package com.xb.p28_messengerdemo;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.Messenger;
import android.os.RemoteException;
import android.telephony.SubscriptionInfo;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    final static String TAG="Messenger";
    TextView textView;

    //服务发送来的信使
    Messenger recvMessenger=null;   //接收服务器的信使
    Messenger mMessenger=null;   //客户端自己信使，传送给服务器

    Handler handler=new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {
            switch (msg.what){
                case MyService.MSG_SET_HELLO:
                    //处理服务器发过来的消息
                    textView.setText(msg.getData().getString("data2"));
                    break;
                default:
                    super.handleMessage(msg);
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        textView=findViewById(R.id.tv_show);
        findViewById(R.id.btn_send).setOnClickListener(new MyClick());
        mMessenger=new Messenger(handler);
    }


    ServiceConnection conn=new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            Log.i(TAG,"连接成功");
            //获得来自服务的信使
            recvMessenger=new Messenger(service);
            mbind=true;
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            Log.i(TAG,"连接断开");
            mbind=false;
        }
    };


    public class MyClick implements View.OnClickListener{

        @Override
        public void onClick(View v) {
            switch (v.getId()){
                case R.id.btn_send:

                    if(!mbind) return;
                    Message message=Message.obtain(null,MyService.MSG_SEND_HELLO);
                    Bundle bundle =new Bundle();
                    bundle.putString("data","客户端说：今天又是充满希望的一天");
                    message.setData(bundle);

                    //1.给服务端发送信息的时候捎带自身的的信封
                    message.replyTo=mMessenger;//给服务一个自己的信封
                    //客户端向服务端发送消息
                    try {
                        recvMessenger.send(message);
                    } catch (RemoteException e) {
                        e.printStackTrace();
                    }
                    break;
            }
        }
    }


    //在启动的时候链接
    boolean mbind=false;
    @Override
    protected void onStart() {
        super.onStart();
        Intent intent=new Intent(this,MyService.class);
        bindService(intent,conn, Context.BIND_AUTO_CREATE);
    }

    @Override
    protected void onStop() {
        super.onStop();
        if(mbind){
            unbindService(conn);
        }
    }
}